Imports System.Windows.Forms

Public Class dlgMapCardNumber

    Public CardNumber As String
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub dlgMapCardNumber_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dgvEmployee.DataSource = db.getvalue("select EmployeeID,eFullName as 'FullName' from Employee where ecardnumber is NULL or (ecardnumber in (select CardNumberID from CardNumber where cActive = 0 )) or (not(ecardnumber in (select CardNumberID from CardNumber ))) ").Tables(0)
        dgvEmployee.ReadOnly = True
        dgvEmployee.Columns(0).Width = 80
        dgvEmployee.Columns(1).Width = 200
        txtfind.Text = ""
    End Sub

    Private Sub dgvEmployee_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEmployee.CellContentDoubleClick
        If e.RowIndex >= 0 And e.ColumnIndex >= 0 Then
            Dim employeeID As String = ""

            Try
                employeeID = dgvEmployee.Item("EmployeeID", e.RowIndex).Value
                db.update("update Employee set eCardnumber = '" & CardNumber & "' where EmployeeID = '" & employeeID & "'")
                MsgBox("The Card Number " & CardNumber & " is set to " & employeeID)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click
        dgvEmployee.DataSource = db.getvalue("select EmployeeID,eFullName as 'FullName' from Employee where (employeeid like '%" & txtfind.Text.Replace("'", "''") & "%' or eFullName like '%" & txtfind.Text.Replace("'", "''") & "%') and (ecardnumber is NULL or (ecardnumber in (select CardNumberID from CardNumber where cActive = 0 )) or (not(ecardnumber in (select CardNumberID from CardNumber )))) ").Tables(0)
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        dlgEmployee.setActionName("New")
        dlgEmployee.ShowDialog()
        btnfind_Click(sender, e)
    End Sub
End Class
